package me.wangshier.pdf;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelImageSaver {
    public static void saveImageToExcel(String imagePath, String excelPath) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Images");

        // 读取图片并添加到Excel
        FileInputStream imageStream = new FileInputStream(imagePath);
        int pictureIdx = workbook.addPicture(imageStream.readAllBytes(), Workbook.PICTURE_TYPE_PNG);
        imageStream.close();

        CreationHelper helper = workbook.getCreationHelper();
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(0);
        anchor.setRow1(0);
        drawing.createPicture(anchor, pictureIdx);

        // 保存Excel文件
        FileOutputStream fileOut = new FileOutputStream(excelPath);
        workbook.write(fileOut);
        fileOut.close();
        workbook.close();
    }
}